package cn.com.api.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import cn.com.api.entity.ApiU_match_prize;
import cn.com.api.mapper.base.U_match_prizeMapperBase;

//DAO
public interface ApiU_match_prizeMapper extends U_match_prizeMapperBase {

	// 新增对象后返回记录ID
	public int U_match_prizeInsertion(ApiU_match_prize u_match_prize);

	// 删除奖项根据赛事ID
	@Delete("DELETE FROM U_MATCH_PRIZE  WHERE MATCH_ID = #{match_id}")
	public void deleteU_match_prizeByMatchId(Integer match_id);

	@Insert("INSERT INTO U_MATCH_PRIZE (MATCH_ID, PRIZE_NAME, PRIZE_NUM, PRIZE_MONEY, WINNER_COUNT, "
			+ "PRIZE_PERCENT, REMARK) VALUES "
			+ "(#{match_id, jdbcType=VARCHAR}, #{prize_name, jdbcType=VARCHAR}, #{prize_num, jdbcType=VARCHAR}, #{prize_money, jdbcType=VARCHAR}, #{winner_count, jdbcType=VARCHAR}, #{prize_percent, jdbcType=VARCHAR},"
			+ " #{remark, jdbcType=VARCHAR})")
	@Options(useGeneratedKeys = true, keyProperty = "prize_id")
	public void insertU_match_prize_info(ApiU_match_prize match_prize);

	public ApiU_match_prize findMatchprizeInfo(Integer prize_id);

	@Select("select a.* , count(w.works_id)as prize_count from (SELECT prize_id, match_id, prize_name, prize_num FROM U_match_prize where match_id = #{match_id, jdbcType=VARCHAR} )a"
			+ " left join U_match_works w on a.prize_id = w.prize_id group by a.prize_id, a.match_id, a.prize_name, a.prize_num order by a.prize_num asc")
	public List<ApiU_match_prize> findU_match_prizesByMatchId(@Param("match_id") String match_id);

	@Select("select count(*) from U_match_works where match_id = #{match_id, jdbcType=VARCHAR} and prize_id = '0'")
	public int findU_match_prize_countByMatchId(@Param("match_id") String match_id);

}
